package com.shou.trigger.http;

import com.shou.api.model.dto.DataAnalysisDTO;
import com.shou.api.model.dto.PageQueryDTO;
import jakarta.annotation.Resource;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;

import java.util.List;

/**
 * ClassName:AbstructController
 * Project:SmartPark
 * Package: com.shou.trigger.http
 * Description
 *
 * @Author Nemo
 * @Create 2025/2/8 17:11
 * @Version 1.0
 */
public class AbstructController {
    @Resource
    private Validator validator;

    /**
     * 根据注解，手动触发对查询参数的入参校验
     */
    public void validateQueryParams(PageQueryDTO pageQueryDTO) {
        BindingResult bindingResult = new BeanPropertyBindingResult(pageQueryDTO, "pageQueryDTO");
        validator.validate(pageQueryDTO, bindingResult);
        if (bindingResult.hasErrors()) {
            // 收集错误信息
            List<String> errorMessages = bindingResult.getAllErrors().stream()
                    .map(ObjectError::getDefaultMessage)
                    .toList();
            throw new RuntimeException("入参校验失败: " + errorMessages);
        }
    }

    public void validateDataAnalysisParams(DataAnalysisDTO dataAnalysisDTO) {
        BindingResult bindingResult = new BeanPropertyBindingResult(dataAnalysisDTO, "dataAnalysisDTO");
        validator.validate(dataAnalysisDTO, bindingResult);
        if (bindingResult.hasErrors()) {
            // 收集错误信息
            List<String> errorMessages = bindingResult.getAllErrors().stream()
                    .map(ObjectError::getDefaultMessage)
                    .toList();
            throw new RuntimeException("入参校验失败: " + errorMessages);
        }
    }
}
